package sncf.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import sncf.client.Train;

public class Converter {

	public static int convertStringDayToMinutes(String extractedValue) {
		int res = 0;

		if (extractedValue != null && !extractedValue.isEmpty()) {
			int hour = 0;
			int min = 0;

			String[] split = null;
			if (extractedValue.contains("h")) {
				split = extractedValue.split("h");
				if (split != null && split.length == 2) {
					hour = Integer.parseInt(split[0]);
				}
			}

			if (split == null) {
				extractedValue = extractedValue.replaceAll(" ", "");
				String val = extractedValue.replace("min", "");
				min = Integer.parseInt(val);
			} else if (split.length == 2) {
				extractedValue = split[1].replaceAll(" ", "");
				String val = extractedValue.replace("min", "");
				min = Integer.parseInt(val);
			}

			res = hour * 60 + min;
		}

		return res;
	}

	public static int convertTimeToDayMinutes(Date date) {
		int res = 0;

		if (date != null) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);

			int hour = Math.max(cal.get(Calendar.HOUR_OF_DAY), 0);
			int min = Math.max(cal.get(Calendar.MINUTE), 0);
			String textTimeFormat = "";

			if (hour > 0) {
				textTimeFormat += hour + "h";
			}
			textTimeFormat += min < 10 ? "0" + min + "min" : min + "min";

			res = Converter.convertStringDayToMinutes(textTimeFormat);
		}

		return res;
	}

	public static String convertTrainListToHtmlNotificationEmail(
			Collection<Train> trainList) {
		StringBuffer htmlEmailContent = new StringBuffer();

		if (trainList != null && !trainList.isEmpty()) {
			final StringBuffer bufferBeforeList = new StringBuffer();
			bufferBeforeList
					.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta name=\"author\" content=\"SNCF Dumper team\">");
			bufferBeforeList
					.append("<title>Gare SNCF de Rouen Rive Droite: Liste des trains en retard</title>");
			bufferBeforeList
					.append("<style>.tvs_tr_grand_vert_clair{background-color:#559E20;}.tvs_tr_grand_vert_fonce{background-color:#0A6D2F;}");
			bufferBeforeList
					.append("tr{color: white;font-size: 20px;font-family: Times New Roman;}th {font-size: 21px;}th, td {text-align: center;padding: 10px;}");
			bufferBeforeList
					.append("thead {background-color:black;}.msg_text {color: black;font-size: 17px;font-family: Arial;}.tvs_td_heure {color: #ff0;}");
			bufferBeforeList.append("</style>");
			bufferBeforeList.append("</head><body>");
			// bufferBeforeList
			// .append("<div class=\"msg_text\"><p>Bonjour<br /><br />Voici vos trains en retard:<br /></p></div>");
			bufferBeforeList
					.append("<table class=\"tab_horaires_tps_reel\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><thead>"
							+ "<tr><th id=\"marque_transporteur\" colspan=\"2\">Transporteur</th><th id=\"numero_id\"><abbr title=\"num&eacute;ro\">Num</abbr></th>"
							+ "<th id=\"heure_id\">Heure</th><th id=\"originedestination_id\">Provenance</th><th id=\"situation_id\" scope=\"col\">Information</th>"
							+ "<th id=\"voie_id\">Voie</th></tr></thead>");

			bufferBeforeList.append("<tbody>");

			final StringBuffer bufferAfterList = new StringBuffer();
			// bufferAfterList
			// .append("</tbody></table><div class=\"msg_text\"><p>L'&eacute;quipe SNCF Dumper</p></div></body></html>");

			bufferAfterList.append("</tbody></table></body></html>");

			htmlEmailContent.append(bufferBeforeList);
			boolean darkColor = false;
			for (Train train : trainList) {
				htmlEmailContent.append("<tr class=\""
						+ (darkColor ? "tvs_tr_grand_vert_fonce"
								: "tvs_tr_grand_vert_clair") + "\">");

				// htmlEmailContent
				// .append("<td class=\"tvs_td_marque\"><img src=\"http://91.68.209.8/bmi/www.gares-en-mouvement.com/resources/images/tvs/sncf_logo_vert_"
				// + (darkColor ? "fonce" : "clair")
				// + ".gif\" /></td>");

				htmlEmailContent
						.append("<td class=\"tvs_td_marque\">SNCF</td>");
				htmlEmailContent.append("<td class=\"tvs_td_type\">"
						+ train.getType() + "</td>");
				htmlEmailContent.append("<td class=\"tvs_td_numero\">"
						+ train.getNumber() + "</td>");

				SimpleDateFormat dateHourFormatter = new SimpleDateFormat("HH");
				SimpleDateFormat dateMinFormatter = new SimpleDateFormat("mm");
				htmlEmailContent.append("<td class=\"tvs_td_heure\">"
						+ dateHourFormatter.format(train.getDateHeure())
						+ "<abbr title=\"heure\">h</abbr>"
						+ dateMinFormatter.format(train.getDateHeure())
						+ "</td>");
				htmlEmailContent
						.append("<td class=\"tvs_td_originedestination\">"
								+ train.getFrom() + "</td>");
				htmlEmailContent.append("<td class=\"tvs_td_situation\">"
						+ train.getSituation() + "</td>");
				htmlEmailContent.append("<td class=\"tvs_td_voie\">"
						+ train.getTrack() + "</td>");

				htmlEmailContent.append("</tr>");
			}
			htmlEmailContent.append(bufferAfterList);
		}

		return htmlEmailContent.toString();
	}
}
